package com.ruoyi.shopping.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.shopping.domain.Commodity;
import com.ruoyi.shopping.service.ICommodityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品管理Controller
 *
 * @author ysq
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/shopping/commodity")
public class CommodityController extends BaseController {
    @Autowired
    private ICommodityService commodityService;

    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Commodity commodity) {
        startPage();
        List<Commodity> list = commodityService.selectCommodityList(commodity);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Commodity commodity) {
        List<Commodity> list = commodityService.selectCommodityList(commodity);
        ExcelUtil<Commodity> util = new ExcelUtil<Commodity>(Commodity.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commodityService.selectCommodityById(id));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commodity commodity) {
        return toAjax(commodityService.insertCommodity(commodity));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commodity commodity) {
        return toAjax(commodityService.updateCommodity(commodity));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commodityService.deleteCommodityByIds(ids));
    }

    /**
     * 获取热点数据
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/hot")
    public AjaxResult hot() {
        return success(commodityService.getHotCommodity());
    }

    /**
     * 获取热点数据(限制三条)
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/hotLimit")
    public AjaxResult hotLimit() {
        return success(commodityService.getHotLimitCommodity());
    }

    /**
     * 获取新品数据(限制三条)
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/newLimit")
    public AjaxResult newCommodity() {
        return success(commodityService.getNewLimitCommodity());
    }
    /**
     * 获取新品数据(限制三条)
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/new")
    public AjaxResult newLimitCommodity() {
        return success(commodityService.getNewCommodity());
    }
    /**
     * 购买商品增加销量减少库存
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/purchase/{id}/{count}")
    public AjaxResult purchase(@PathVariable("id") Long id, @PathVariable("count") Integer count) {
        System.out.println(id + " " + count);
        return success(commodityService.purchase(id, count));
    }
    /**
     * 商品搜索
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/search")
    public AjaxResult commoditySearch(String description) {
        return success(commodityService.selectSearch(description));
    }
    /**
     * 商品搜索
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodity:list')")
    @GetMapping("/hotSearch")
    public AjaxResult HotSearch() {
        return success(commodityService.hotSearch());
    }

}
